package Tree;

public class item654 {
    public static TreeNode constructMaximumBinaryTree(int[] nums) {
        if(nums.length==0) return null;
       TreeNode root= build(nums,0,nums.length-1);
        return  root;
    }

    private static TreeNode build(int[] nums, int left, int right) {
        if(left>right) return null;
        int max = 0;
        int index =0;
        for (int i = left; i <=right ; i++) {
            if(nums[i]>=max)
            {
                max = nums[i];
                index = i;
            }
        }
        TreeNode root = new TreeNode(max);
       root.left= build(nums,left,index-1);
       root.right= build(nums,index+1,right);
        return root;
    }

    public static void main(String[] args) {
        int []nums = {3,2,1,6,0,5};
        constructMaximumBinaryTree(nums);
    }

}
